package zozopo.warehouse;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.ReceiptOS;
import zozopo.warehouse.operations.ReceiptTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class GetGoodsSellInfoForAjaxAction extends ActionSupport implements Action,SessionAware,ServletRequestAware,ServletResponseAware{
	//传入参数
	private int goodsid;

	//传出参数
	private String warehouseinfo = "";
	private int lastSellNumber;
	private double lastSellPrice;
	
	HttpServletRequest request;
	HttpServletResponse response;
	private Map<Object, Object> session;
	
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}

	

	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		response = arg0;
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=UTF-8");
	}
	
	public String execute(){
		
		ReceiptOS receiptOS = new ReceiptOS();
		PurchaseOS purchaseOS = new PurchaseOS(receiptOS.getConn());
		
		Map<String,String> warehouseinfoMap = purchaseOS.getGoodsWarehouseInfo(goodsid);
		warehouseinfo+="<select name='warehouse' id='warehouse'>";
		for(Map.Entry<String, String> m : warehouseinfoMap.entrySet()){
			warehouseinfo+="<option value='"+m.getKey()+"'>"+m.getValue()+"</option>";	
		}
		warehouseinfo+="</select>";
		
		//JSONObject jsonObject = new JSONObject(warehouseinfoMap);
		//warehouseinfo = jsonObject.toString();
		
			//GoodsTB goods = goodsOS.getGoodsById(goodsid);
			
		ReceiptTB receipt = receiptOS.getReceiptForGoods(goodsid);
		if(receipt == null){
				receipt = new ReceiptTB();
				receipt.setNumber_receipt(0);
				receipt.setPrice_receipt(0.00);
		}
			
		lastSellNumber = receipt.getNumber_receipt();
		lastSellPrice = receipt.getPrice_receipt();
		receiptOS.releaseConnection();
		
		
		return SUCCESS;
	}

	public int getGoodsid() {
		return goodsid;
	}

	public void setGoodsid(int goodsid) {
		this.goodsid = goodsid;
	}

	public String getWarehouseinfo() {
		return warehouseinfo;
	}

	public void setWarehouseinfo(String warehouseinfo) {
		this.warehouseinfo = warehouseinfo;
	}

	public int getLastSellNumber() {
		return lastSellNumber;
	}

	public void setLastSellNumber(int lastSellNumber) {
		this.lastSellNumber = lastSellNumber;
	}

	public double getLastSellPrice() {
		return lastSellPrice;
	}

	public void setLastSellPrice(double lastSellPrice) {
		this.lastSellPrice = lastSellPrice;
	}

	
}
